package com.gt.web.controller.system;

import java.util.List;
import java.util.Set;
import java.util.UUID;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gt.sso.domain.SysSso;
import com.gt.sso.service.ISysSsoService;
import com.gt.sso.vo.SsoLoginUser;
import com.gt.sso.vo.SsoRegister;
import com.gt.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.gt.common.constant.Constants;
import com.gt.common.core.domain.AjaxResult;
import com.gt.common.core.domain.entity.SysMenu;
import com.gt.common.core.domain.entity.SysUser;
import com.gt.common.core.domain.model.LoginBody;
import com.gt.common.utils.SecurityUtils;
import com.gt.framework.web.service.SysLoginService;
import com.gt.framework.web.service.SysPermissionService;
import com.gt.system.service.ISysMenuService;

/**
 * 登录验证
 *
 * @author fish
 */
@Api(tags = "sys-登录验证")
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysSsoService ssoService;

    @Autowired
    private ISysUserService sysUserService;

//    @ApiOperation("sso注册/同步账号")
    @PostMapping("/ssoRegister")
    public AjaxResult ssoRegister(SsoRegister ssoRegister){
        boolean result = false;

        //验证秘钥
        SysSso sysSso = ssoService.getOne(Wrappers.<SysSso>lambdaQuery().eq(SysSso::getSysId, ssoRegister.getSysId()));
        if(sysSso == null){return AjaxResult.success(false);}

        //同步用户
        SysUser sysUser = BeanUtil.copyProperties(ssoRegister,SysUser.class);
        SysUser user = sysUserService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUserName, sysUser.getUserName()));
        if(user == null){
            result = sysUserService.save(sysUser);
        }else{
            SysUser updateUser = new SysUser();
            updateUser.setSsoUsername(sysUser.getSsoUsername());
            result = sysUserService.update(updateUser,Wrappers.<SysUser>lambdaUpdate().eq(SysUser::getUserId,user.getUserId()));
        }

        return AjaxResult.success(result);
    }

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("登录方法")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("测试使用无需验证码")
    @PostMapping("/h5Login")
    public AjaxResult h5Login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    public static void main(String[] args) {
        System.out.println(UUID.randomUUID().toString().replaceAll("-",""));
    }

    /**
     * SSO登录
     *
     * @param ssoLoginUser
     * @return 结果
     */
//    @ApiOperation("SSO登录")
    @PostMapping("/ssoLogin")
    public AjaxResult usernameLogin(SsoLoginUser ssoLoginUser)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.ssoLogin(ssoLoginUser);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
